/**
 * Created by zhangpeng on 2016/8/16.
 */
app.controller('blackListCtrl', function($scope, BlackListService){
    var msgBox = null;
    var searchGWUserId = '0';

    /**
     * 用户名下拉框加载数据
     */
    BlackListService.getBGSUsers({
        action : 'getBGSUsers'
    },function(data){
        //查询所有BGS用户
        $scope.userConfigsList = data;
    });

    /**
     * 页面初始化加载
     */
    BlackListService.getAllBlackListByCondition({
        action: 'getAllBlackListByCondition',
        currentPage : 1,
        pageSize : 15,
        gwUserId : 0
    }, function(data){
        //展示数据
        if(data.state == 'SUCCESS'){
            $scope.blackLists = data.data.BlackListVO;
            pageOperation($scope,data.pageInfo);
        }else{
            if(msgBox == null) {
                message_alert('提示',data.reason, function() {
                    msgBox == null
                });
            }
        }
    });

    /**
     * 通过查询按钮查询
     * @param object
     */
    $scope.findByConditions = function(object){
        searchGWUserId =$scope.searchGWUserId;
        $scope.pageSize = $scope.selectPageSize
        BlackListService.getAllBlackListByCondition({
            action : 'getAllBlackListByCondition',
            gwUserId : searchGWUserId,
            currentPage : 1,
            pageSize : $scope.pageSize
        },function(data){
            if(data.state == 'SUCCESS'){
                $scope.blackLists = data.data.BlackListVO;
                pageOperation($scope,data.pageInfo);
            }else{
                message_alert('提示',ConstantUtil.getConstant('SEARCH_FAIL') + "(" + data.reason + ")",null);
            }
        });
    }

    /**
     * 通过分页按钮
     * @param object
     */
    $scope.findByCondition = function(object){
        $scope.pageSize = $scope.selectPageSize
        BlackListService.getAllBlackListByCondition({
            action : 'getAllBlackListByCondition',
            gwUserId : searchGWUserId,
            currentPage : $scope.selPage,
            pageSize : $scope.pageSize
        },function(data){
            if(data.state == 'SUCCESS'){
                $scope.blackLists = data.data.BlackListVO;
                pageOperation($scope,data.pageInfo);
            }else{
                message_alert('提示',ConstantUtil.getConstant('SEARCH_FAIL') + "(" + data.reason + ")",null);
            }
        });
    }

    /**
     * 校验黑名单用户名称
     * @param gwUserId
     */
    $scope.checkIUName = function(gwUserId){
        if($scope.title == '新增黑名单用户'){
            BlackListService.findByGWUserId({
                action : 'findByGWUserID',
                gwUserId : gwUserId
            },function(data) {
                if(data.state != 'SUCCESS'){
                    if(data.code == 0){
                        $scope.formBlackList.gwUserId.$setValidity("unique",false);
                        message_alert('提示',data.reason,null);
                    }
                }else{
                    $scope.formBlackList.gwUserId.$setValidity("unique",true);
                }
            });
        }
    }

    /**
     * 新增黑名单用户
     */
    $scope.createBlackList = function(){
        //先移除样式然后添加
        $('#glyphicon').removeClass("glyphicon glyphicon-pencil");
        $('#glyphicon').removeClass("glyphicon glyphicon-plus");
        $('#glyphicon').addClass("glyphicon glyphicon-plus");
        $('#dialogBlackList').on('show.bs.modal',function(){
            $scope.title = '新增黑名单用户';
            $scope.blackList.id = 0;
            $scope.blackList.gwUserId = '0';
            $scope.blackList.sendAddress = '';
            $scope.blackList.recvAddress = '';
            $scope.blackList.smi = '';
            $scope.blackList.an = '';
            $scope.blackList.specLable = '';
            $scope.blackList.index = '0';
            $scope.formBlackList.gwUserId.$setValidity("unique",true);
            $("#gwUserId").removeAttr('disabled');
        });
        $('#dialogBlackList').modal('show');
    }

    /**
     * 修改黑名单用户
     * @param blackList
     */
    $scope.updateBlackList = function(blackList){
        //先移除样式然后添加
        $('#glyphicon').removeClass("glyphicon glyphicon-pencil");
        $('#glyphicon').removeClass("glyphicon glyphicon-plus");
        $('#glyphicon').addClass("glyphicon glyphicon-pencil");
        $('#dialogBlackList').on('show.bs.modal',function(){
            $scope.title = '修改黑名单用户';
            $scope.blackList.id = blackList.id;
            $scope.blackList.gwUserId = blackList.gwUserId.toString();
            $scope.blackList.sendAddress = blackList.sendAddress;
            $scope.blackList.recvAddress = blackList.recvAddress;
            $scope.blackList.smi = blackList.smi;
            $scope.blackList.an = blackList.an;
            $scope.blackList.specLable = blackList.specLable;
            $scope.blackList.index = blackList.index;
            $('#gwUserId').attr('disabled','disabled');
        });
        $('#dialogBlackList').modal('show');
    }

    $scope.close = function(){
        $('#dialogBlackList').modal('hide');
    }

    /**
     * 文本框校验 获取焦点为false 丢失焦点为true
     */
    $scope.change = function (flag) {
        $scope.changed = flag;
    }

    /**
     * 保存黑名单用户
     * @param blackList
     */
    $scope.save = function(blackList){
        if(!((blackList.sendAddress==null || blackList.sendAddress=='') && (blackList.recvAddress==null || blackList.recvAddress=='') && (blackList.smi==null || blackList.smi=='') && (blackList.an==null || blackList.an=='') && (!((blackList.specLable!=null && blackList.specLable!='') && (blackList.index!=null )))) ) {
            if (blackList.id == 0) {
                if (blackList.gwUserId == '0') {
                    message_alert('提示','请选择黑名单用户!',null);
                } else {
                    BlackListService.save({
                        gwUserId : blackList.gwUserId,
                        sendAddress : blackList.sendAddress,
                        recvAddress: blackList.recvAddress,
                        smi: blackList.smi,
                        an: blackList.an,
                        specLable : blackList.specLable,
                        index : blackList.index
                    },{},function(data) {
                        if(data.state == 'SUCCESS'){
                            $scope.close();
                            message_alert('提示',ConstantUtil.getConstant('SAVE_SUCCESS'), function(){
                                $scope.findByCondition();
                                message_alert('提示','黑名单配置新增成功后,请记得重新推送!',null);
                            });
                        }else{
                            message_alert('提示',ConstantUtil.getConstant('SAVE_FAIL') + "(" + data.reason + ")",null);
                        }
                    }, $scope.requestError);
                }
            } else {
                BlackListService.updateBlackList({
                    id : blackList.id,
                    gwUserId : blackList.gwUserId,
                    sendAddress : blackList.sendAddress,
                    recvAddress: blackList.recvAddress,
                    smi: blackList.smi,
                    an: blackList.an,
                    specLable: blackList.specLable,
                    index: blackList.index
                },function(data) {
                    if(data.state == 'SUCCESS'){
                        $scope.close();
                        message_alert('提示',ConstantUtil.getConstant('SAVE_SUCCESS'), function(){
                            $scope.findByCondition();
                            message_alert('提示','黑名单配置修改成功后,请记得重新推送!',null);
                        });
                    }else{
                        message_alert('提示',ConstantUtil.getConstant('SAVE_FAIL') + "(" + data.reason + ")",null);
                    }
                });
            }
        } else {
            message_alert('提示','发送地址、接收地址、SMI、机尾号、(全文关键字、起始位)，至少需要选择一项；起始位不允许为空！',null);
        }
    }

    /**
     * 删除黑名单用户
     * @param id
     */
    $scope.deleteBlackList = function(id){
        message_confirm('提示','确认是否要删除该黑名单？',
            function (result) {
                if(result) {
                    BlackListService.delete({
                        id: id
                    }, function  (data) {
                        if (data.state == 'SUCCESS') {
                            message_alert('提示',ConstantUtil.getConstant('OPERATE_SUCCESS'), function () {
                                $scope.findByCondition();
                                message_alert('提示','黑名单配置删除成功后,请记得重新推送!',null);
                            });
                        }else{
                            message_alert('提示',ConstantUtil.getConstant('OPERATE_FAIL') + "(" + data.reason + ")",null);
                        }
                    }, $scope.requestError);
                }
            });
    }

    /**
     * 黑名单用户配置推送
     */
    $scope.push = function() {
        //$("#dialogTip").modal("show");
        BlackListService.push({
            action : 'push'
        },{},function(data) {
            //$('#dialogTip').modal('hide');
            if(data.state == 'SUCCESS'){
                message_alert('提示',ConstantUtil.getConstant('OPERATE_SUCCESS'),function(){
                    $scope.findByCondition();
                });
            }else{
                message_alert('提示',ConstantUtil.getConstant('OPERATE_FAIL') + "(" + data.reason + ")",null);
            }
        }, $scope.requestError)
    }

    /**
     * 黑名单用户配置下载
     */
    $scope.download = function() {
        location.href="blackList/downloadBlackList";
    }

});
